package com.masz.lcnpay;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.codingapi.txlcn.tc.annotation.TccTransaction;
import com.masz.lcnpay.mapper.TbPay;
import com.masz.lcnpay.mapper.TbPayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author masz
 * @Date 2022/2/11 01:36
 */
@Service
public class PayService {

    @Autowired
    TbPayMapper payMapper;


    @Transactional(rollbackFor = Exception.class)
    @LcnTransaction
    public void pay(String orderName){
        TbPay pay = new TbPay();
        pay.setPayName(orderName);
        payMapper.insert(pay);
        int i = 1/0;
    }

    /**
     * tcc 事务
     * @param payName
     */
    @Transactional(rollbackFor = Exception.class)
    @TccTransaction
    public void payTcc(String payName) {
        System.out.println("tcc pay:"+payName);
        TbPay pay = new TbPay();
        pay.setPayName(payName);
        payMapper.insert(pay);
        // 模拟异常
        int i = 1/0;
    }

    public void confirmPayTcc(String payName) {
        System.out.println("tcc confirm : "+ payName);
    }

    public void cancelPayTcc(String payName) {
        payMapper.delByPayName(payName);
        System.out.println("tcc cancel : "+ payName);
    }

}
